package com.web.my12306.dao.impl;

import com.web.my12306.dao.IOrderDao;
import com.web.my12306.pojo.Orders;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl extends BaseDao implements IOrderDao {

    @Override
    //按订单id查看订单详情
    /***
     * 黄逸9.12
     ***/
    public Orders queryByOid(int oid) {
        Orders order = new Orders();
        String sql = "select * from orders where oid=?";
        try {
            query(sql, oid);
            while (rs.next()) {
                order.setOid(rs.getInt("oid"));
                //order.setOnumber(rs.getString("onumber"));
                order.setUid(rs.getInt("uid"));
                order.setTrainno(rs.getString("trainno"));
                order.setDeparture_time(rs.getTimestamp("departure_time"));
                //order.setSell_time(rs.getTimestamp("sell_time"));
                order.setArrival_time(rs.getTimestamp("arrival_time"));
                order.setDstation(rs.getString("dstation"));
                order.setTerminal(rs.getString("terminal"));
                order.setPrice(rs.getDouble("price"));
                order.setCarriage(rs.getInt("carriage"));
                order.setSeatno(rs.getString("seatno"));
                order.setState(rs.getInt("state"));
                order.setTid(rs.getInt("tid"));
                order.setSid(rs.getInt("sid"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return order;
    }

    /***
     * 黄逸9.13
     ***/
    @Override
    public void add(Orders order) {
        String sql="insert into orders(onumber,uid,trainno,departure_time,sell_time,arrival_time,dstation,terminal,price,carriage,seatno,state,sid,tid) " +
                "values(?,?,?,?,sysdate(),?,?,?,?,?,?,?,?,?)";
        update(sql,order.getOnumber(),order.getUid(),order.getTrainno(),order.getDeparture_time(),
        order.getArrival_time(),order.getDstation(),order.getTerminal(),order.getPrice(),order.getCarriage(),order.getSeatno(),order.getState(),order.getSid(),order.getTid());
    }

    @Override
    public List<Orders> getAll(int uid) {
        String sql="select * from orders where uid=?";
        query(sql,uid);
        List<Orders> orders=new ArrayList<>();
        try {
            while(rs.next()){
                Orders order = new Orders();
                order.setOid(rs.getInt("oid"));
                order.setOnumber(rs.getString("onumber"));
                order.setUid(rs.getInt("uid"));
                order.setTrainno(rs.getString("trainno"));
                order.setDeparture_time(rs.getTimestamp("departure_time"));
                order.setSell_time(rs.getTimestamp("sell_time"));
                order.setArrival_time(rs.getTimestamp("arrival_time"));
                order.setDstation(rs.getString("dstation"));
               order.setTerminal(rs.getString("terminal"));
                order.setPrice(rs.getDouble("price"));
                order.setCarriage(rs.getInt("carriage"));
                order.setSeatno(rs.getString("seatno"));
                order.setState(rs.getInt("state"));
                order.setSid(rs.getInt("sid"));
                order.setTid(rs.getInt("tid"));
                orders.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return orders;
    }

    @Override
    public List<Orders> getByUserPage(int uid, int index, int num)
    {
        String sql="select * from orders where uid=? limit ?,?";
        query(sql,uid,index,num);
        List<Orders> orders=new ArrayList<>();
        try {
            while(rs.next()){
                Orders order = new Orders();
                order.setOid(rs.getInt("oid"));
                order.setOnumber(rs.getString("onumber"));
                order.setUid(rs.getInt("uid"));
                order.setTrainno(rs.getString("trainno"));
                order.setDeparture_time(rs.getTimestamp("departure_time"));
                order.setSell_time(rs.getTimestamp("sell_time"));
                order.setArrival_time(rs.getTimestamp("arrival_time"));
                order.setDstation(rs.getString("dstation"));
                order.setTerminal(rs.getString("terminal"));
                order.setPrice(rs.getDouble("price"));
                order.setCarriage(rs.getInt("carriage"));
                order.setSeatno(rs.getString("seatno"));
                order.setState(rs.getInt("state"));
                order.setSid(rs.getInt("sid"));
                orders.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return orders;
    }

    @Override
    public void editState(int tid, int state)
    {
        update("UPDATE orders SET state=? WHERE sid=?",state,tid);
    }

    @Override
    public boolean editStateByOid(int oid, int state)
    {
        update("UPDATE orders SET state=? WHERE oid=?",state,oid);
        return true;
    }
}
